$(function(){
	var data={
		
	}
	function loadajax(type,php_url,datas,fn){  //封装ajax
		$.ajax({
			type:type,      
			url:php_url,    
			async:true,     
			data:datas,     
			dataType:'json', 
			success:function(msg){
				if(fn){
					fn(msg);
				}
			},error:function(msg){
				alert('ajax连接失败！');
			}
		});
	}
	
//	主页轮播图
	
	function banners(msg){
		var html='';
		var oLi = '';
		for(var i = 0 ; i < msg.length ;i++){
					if(i == 0){
						oLi += '<li class="active" data-slide-to="'+i+'" data-target="#carousel-135899"></li>';
						html += '<div class="item active"><img alt="" src="'+msg[i]['banner_src'] +'"/></div>';
					}else{
						oLi += '<li data-slide-to="'+i+'" data-target="#carousel-135899"></li>';
						html += '<div class="item"><img alt="" src="'+msg[i]['banner_src'] +'"/></div>';
					}
				}
				$('.carousel-indicators').html(oLi);
				$('.carousel-inner').html(html);
	    }
	
	loadajax('post',"index.php?m=home&c=index&a=getBanner",data,banners);	
	
//  主页产品轮播图	
    function product_banner(msg){
    	var html = '';
				for(var i = 0 ; i < msg.length ;i++){
					html += '<li><a href="'+msg[i]['href']+'" ><img src="'+msg[i]['procat_src']+'"/><h3>'+msg[i]['name']+'</h3></a></li>';
				}
				$('.list').html(html);
				
				var oWidth = $('#bonner .list li').width(); //动态获取获取li宽度
					var oLength = $('#bonner .list li').length; //动态获取li个数
					var oHtml = $('#bonner .list').html();
					$('#bonner .list').html(oHtml + oHtml);
					var oLength1 = $('#bonner .list li').length;
					$('#bonner .list').width(oWidth * oLength1);
					var timer = null;
					var iNow = 0;
					var isFinish = true;
					
					function move(){  //实现轮播
						isFinish = false;
						$('#bonner .list').stop().animate({left:-oWidth*(iNow)},500,function(){
							if(iNow == oLength){
								$('#bonner .list').css({left:0});
								iNow = 0;
							}
							isFinish = true;
						})
					}
					
					function autoPlay(){ //自动轮播
						iNow++;
						move();
					}
					
					timer = setInterval(autoPlay,2000);//定时器调用autoplay实现自动轮播
					
					$('.right').click(function(){ 	//点击右边按钮
						if(isFinish){
							autoPlay();
						}
					});
					$('.left').click(function(){	//点击左边按钮
						if(isFinish){
							if(iNow == 0){
								$('#bonner .list').css({left:-oWidth*oLength});
								iNow = oLength;
							}
							iNow--;
							move();
						}
					});
					$('#bonner').hover(function(){  //鼠标移入停止轮播
						clearInterval(timer);
					},function(){
						timer = setInterval(autoPlay,2000);
					})
				}
    
    loadajax('post',"index.php?m=home&c=index&a=getProduct",data,product_banner);

//  主页关于模块    
    function index_product(msg){
    		var html = ' ';
				html = '<a href="'+'#'+'"><img class="left_1 img-responsive" src="'+msg[0].img_src+'"/></a><p class="right_1">'+msg[0].article+'</p><a href="'+msg[0].href+'">了解更多>></a>';
				$('.content-about').html(html);
    }
    
     loadajax('post',"index.php?m=home&c=article&a=index",data,index_product);

//  主页传世模块
	function fn(msg){
		var html='';
		for (var i=0;i<msg.length;i++) {
			html+='<div class="left_2 col-md-4  col-xs-12"><a href="'+msg[i]['link']+'"><img class="left_2" src="'+msg[i]['img_src']+'"/><h4>'+msg[i]['h3_name']+'</h4><p>'+msg[i]['h4_name']+'</p></a></div>'
		}
		$('.product-content').html(html);
	}
	
	loadajax('post',"index.php?m=home&c=index&a=chuanshi",data,fn);
	
})